import express from 'express';
import { scoringCriteriaController } from '../controllers/scoringCriteriaController';
import { roleAuth } from '../middleware/roleAuth';
import { Role } from '@prisma/client';
import { validateRequest } from '../middleware/validation';
import { z } from 'zod';

const router = express.Router();

// 验证创建和更新评分标准的请求数据
const scoringCriteriaSchema = z.object({
  name: z.string().min(1, '名称不能为空'),
  description: z.string().optional(),
  subjectId: z.string().min(1, '学科ID不能为空'),
  gradeLevel: z.number().min(1, '年级必须为正整数'),
  totalScore: z.number().positive('总分必须为正数'),
  passingScore: z.number().positive('及格分数必须为正数'),
  rubricItems: z.array(z.object({
    name: z.string().min(1, '评分项名称不能为空'),
    description: z.string().min(1, '评分项描述不能为空'),
    scoreRange: z.array(z.number()).min(2, '分数范围必须包含最低和最高分').max(2, '分数范围必须包含最低和最高分'),
    weight: z.number().min(0, '权重不能为负数').max(1, '权重不能超过1')
  })).min(1, '至少需要一个评分项'),
  questionScores: z.array(z.object({
    questionType: z.enum(['MULTIPLE_CHOICE', 'OPEN_ENDED']),
    score: z.number().positive('每题分值必须为正数')
  })).min(1, '至少需要一种题型的分值配置')
});

// 路由配置
router.get('/', roleAuth([Role.ADMIN, 'read:*']), scoringCriteriaController.getScoringCriteria);
router.get('/:id', roleAuth([Role.ADMIN, 'read:*']), scoringCriteriaController.getScoringCriteriaById);
router.post('/', roleAuth([Role.ADMIN]), validateRequest(scoringCriteriaSchema), scoringCriteriaController.createScoringCriteria);
router.put('/:id', roleAuth([Role.ADMIN]), validateRequest(scoringCriteriaSchema), scoringCriteriaController.updateScoringCriteria);
router.delete('/:id', roleAuth([Role.ADMIN]), scoringCriteriaController.deleteScoringCriteria);

export default router;
